<?php

namespace App\Http\Controllers\Api\Web\Sync;

use App\Http\Controllers\Common\ApiController;
use App\Models\Cert\CertDriver;
use App\Models\Cert\CertGo;
use App\Models\Cert\CertGoOther;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class SyncUserController extends ApiController {


    public function getInfo(Request $request) {
        $type = $request->input('type',1);
        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',30);
        $sTime = $request->input('sTime',date('Y-m-d'));
        $eTime = $request->input('eTime',date('Y-m-d'));

        $this->validate($request, [
            'type'=>'required|integer|min:0|max:2',
            'sTime'=>'required|date_format:Y-m-d',
            'eTime'=>'required|date_format:Y-m-d'
        ],[],[
            'type'=>'信息类型 0：行驶证-主页；1：驾驶证 2：行驶证-副页',
            'sTime'=>'开始时间',
            'eTime'=>'结束时间',
        ]);


        if($type==1){
            $instance = new CertDriver();
        }elseif ($type==0){
            $instance = new CertGo();
        }else{
            $instance = new CertGoOther();
        }
        $query = $instance->newQuery();
        $records = $query->whereDate('created_at','>=',$sTime)->whereDate('created_at','<=',$eTime)->paginate($pageSize,['*'],'page',$page);

        foreach ($records as $record) {
            $record->img_url = asset($record->img_url);
            if($type==1){
                $record->car_number = CertGo::where('user_id',$record->user_id)->first()->car_number??'';
            }
        }

        return responseSuccess($records->items(),$records->total());
    }

}
